package com.nlscan.nhub.test

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat.startActivity
import com.nlscan.nhub.test.common.AppConst
import com.nlscan.nhub.test.ui.SendCmdBroadcastActivity
import com.nlscan.nhub.test.ui.theme.NhubTheme

class MainTestActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NhubTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
//                    MyApp()
                    Greeting(this, "Android")
                }
            }
        }
    }


}

@Preview(showBackground = true)
@Composable
fun Preview1() {
    Greeting(context = LocalContext.current, name = "")
}

@Composable
fun Greeting(context: Context, name: String) {

    Column() {

//        Text(text = "Hello $name!")

        Button(
            onClick = { jumpToNhubMainPage(context) },
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 16.dp, end = 16.dp)
        ) {
            Text(
                text = stringResource(id = R.string.jump_to_nhub_main_page)

            )
        }
        Button(
            onClick = { jumpToNhubScanToConnectPage(context) },
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 16.dp, end = 16.dp)
        ) {
            Text(
                text = stringResource(id = R.string.jump_to_nhub_scan_connect_page)
            )
        }

        //Test 发送测试广播
        Button(
            onClick = {
//                val intent = Intent(AppConst.ACTION_SEND_NLS_CMD)
//                intent.putExtra(AppConst.EXTRA_NLS_CMD,"@WLSQPW")
//                context.sendBroadcast(intent)
//                Toast.makeText(context, "发送广播测试", Toast.LENGTH_SHORT)
//                    .show()

                val intent = Intent(context, SendCmdBroadcastActivity::class.java)
                startActivity(context,intent,null)

            },
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 16.dp, end = 16.dp)
        ) {
            Text(
                text = "跳转到发送指令广播测试页面"
            )
        }

    }
}

private fun jumpToNhubMainPage(context: Context) {
    val pkgName = "com.nlscan.ncomgateway"
    if (isPackageInstalled(context, pkgName)) {
        val intent = context.packageManager.getLaunchIntentForPackage(pkgName)
        context.startActivity(intent)
    } else {
        Toast.makeText(context, context.getString(R.string.nhub_no_install), Toast.LENGTH_SHORT)
            .show()
    }
}

private fun isPackageInstalled(context: Context, pkgName: String): Boolean {
    return try {
        context.packageManager.getPackageInfo(pkgName, 0)
        true
    } catch (e: Exception) {
        false
    }
}


private fun jumpToNhubScanToConnectPage(context: Context) {
    val pkgName = "com.nlscan.ncomgateway"
    if (isPackageInstalled(context, pkgName)) {
        val intent = Intent()
        intent.setClassName(
            pkgName,
            "com.nlscan.ncomgateway.feature.device.add.view.ShowConnectCodeActivity"
        )
        try {
            context.startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(
                context,
                context.getString(R.string.no_permission_to_jump),
                Toast.LENGTH_SHORT
            )
                .show()
        }
    } else {
        Toast.makeText(context, context.getString(R.string.nhub_no_install), Toast.LENGTH_SHORT)
            .show()
    }
}


//@Preview(showBackground = true)
//@Composable
//fun DefaultPreview() {
//    NhubTheme {
//        Greeting(context = ,"Android")
//    }
//}

//@Composable
//fun TopBar(onMenuClicked: () -> Unit) {
//    TopAppBar(
//        title = {
//            Text(text = "Scaffold Example", color = Color.White)
//        },
//        navigationIcon = {
//            Icon(
//                imageVector = Icons.Default.Menu,
//                contentDescription = "Menu",
//                modifier = Modifier.clickable(onClick = onMenuClicked),
//                tint = Color.White
//            )
//        },
//        backgroundColor = Purple700,
//        elevation = 12.dp
//    )
//}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
//@Preview(showBackground = true)
//@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
//@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyApp() {

    Scaffold(
        topBar = {
            MediumTopAppBar(
                title = {
                    Text(text = stringResource(id = R.string.app_name), color = Color.Green)
                },
                modifier = Modifier.background(Color.Blue)
//                navigationIcon = {
//                    Icon(
//                        imageVector = Icons.Default.Menu,
//                        contentDescription = "Menu",
//                    )
//                },

//                colors = {
//                    TopAppBarColors(
//
//                    ) {
//
//                    }
//                },
//                modifier = {
//                    back
//                }
//                backgroundColor = Purple700,
//                elevation = 12.dp
            )


        },
        content = {

        }
    )


}

//fun Scaffold(topBar: () -> Unit) {
//    TODO("Not yet implemented")
//}

//@ExperimentalMaterial3Api
//@Composable
//fun MyApp2() {
//    Scaffold(
//        topBar = {
//            SmallTopAppBar(
//                title = { Text(text = "My App") },
//                actions = {
//                    // Action items here
//                }
//            )
//        },
////        content = {
////            // Content of the screen here
////        }
//    )
//}


